<?php 
require_once("perficature-utils.php");
insertHeader("Confirm User-Agent");


// Handle any actions FIRST!
if ( array_key_exists('submit', $_GET) ) {
	$aKeys = array_keys($_GET);
	for ( $i = 0; $i < count($aKeys); $i++ ) {
		$key = $aKeys[$i];
		if ( ereg("rb_([0-9]+)", $key, $regs) ) {
			$testid = $regs[1];
			$value = $_GET[$key];
			if ( 'con' == $value ) {
				confirmUA($testid);
			}
			else if ( 'rem' == $value ) {
				removeUA($testid);
			}
			else if ( 'cha' == $value ) {
				changeUA($testid);
			}
		}
	}
}

// Now draw remaining user agents.
$giCounter = 1;
$gnPaginate = 10;
$gsHtml = "<table border=1 cellpadding=8 cellspacing=0>\n";
$gsPbrowser = ( array_key_exists('pbrowser', $_GET) ? $_GET['pbrowser'] : "" );
$gsUseragent = ( array_key_exists('useragent', $_GET) ? $_GET['useragent'] : "" );
$gbConfirmed = ( array_key_exists('confirmed', $_GET) && 'on' == $_GET['confirmed'] );

$gsPbCond = ( $gsPbrowser ? 
			  ( 0 === strpos($gsPbrowser, '!') ?
				"pbrowser not regexp '" . substr($gsPbrowser, 1) . "' and" : 
				"pbrowser regexp '$gsPbrowser' and" ) :
			  "" );
$gsUACond = ( $gsUseragent ? 
			  ( 0 === strpos($gsUseragent, '!') ?
				"useragent not regexp '" . substr($gsUseragent, 1) . "' and" : 
				"useragent regexp '$gsUseragent' and" ) :
			  "" );
$gsConfCond = ( $gbConfirmed ? "confirmed>=0" : "confirmed=0" );
$ghDoubleCheck = array();

// UNKNOWN
$query = "select testid, useragent, count(useragent) as num, pbrowser, browser, v1, v2, v3 from $gTestsTable where pbrowser='unknown' and $gsPbCond $gsUACond $gsConfCond group by useragent order by pbrowser, useragent;";
$result = doQuery($query);
$tmp = "";
while ($row = mysql_fetch_assoc($result)) {
	$tmp .= drawUseragent($row['num'], $row['testid'], $row['useragent'], $row['pbrowser']);
	$giCounter++;
	if ( $giCounter > $gnPaginate ) {
		break;
	}
}
mysql_free_result($result);
if ( $tmp ) {
	$gsHtml .= "<tr><td colspan=5 style='border-color: white; font-weight: bold; font-size: 1.4em;'>Unknown User Agents</td></tr>\n" .
		"<tr> <th>UA Profiler's Classification</th> <th>useragentstring.com's Classification</th> <th>user-agent-string.info's Classification</th> <th>Actual User-Agent</th> <th>#Tests</th> <th>Actions</th> </tr>\n" .
		"$tmp";
}


// SINGLE OCCURRANCE
if ( $giCounter < $gnPaginate ) {
	$query = "select testid, useragent, count(useragent) as num, pbrowser, browser, v1, v2, v3 from $gTestsTable where pbrowser!='unknown' and $gsPbCond $gsUACond $gsConfCond group by useragent having num=1 order by pbrowser, useragent;";
	$result = doQuery($query);
	$tmp = "";
	while ($row = mysql_fetch_assoc($result)) {
		$tmp .= drawUseragent($row['num'], $row['testid'], $row['useragent'], $row['pbrowser']);
		$giCounter++;
		if ( $giCounter > $gnPaginate ) {
			break;
		}
	}
	mysql_free_result($result);
	if ( $tmp ) {
		$gsHtml .= "<tr><td colspan=5 style='border-color: white; font-weight: bold; font-size: 1.4em;'>Unique User Agents</td></tr>\n" .
		"<tr> <th>UA Profiler's Classification</th> <th>useragentstring.com's Classification</th> <th>user-agent-string.info's Classification</th> <th>Actual User-Agent</th> <th>#Tests</th> <th>Actions</th> </tr>\n" .
		"$tmp";
	}
}


// REMAINING
if ( $giCounter < $gnPaginate ) {
	$query = "select testid, useragent, count(useragent) as num, pbrowser, browser, v1, v2, v3 from $gTestsTable where pbrowser!='unknown' and $gsPbCond $gsUACond $gsConfCond group by useragent having num > 1 order by pbrowser, useragent;";
	$result = doQuery($query);
	$tmp = "";
	while ($row = mysql_fetch_assoc($result)) {
		$tmp .= drawUseragent($row['num'], $row['testid'], $row['useragent'], $row['pbrowser']);
		$giCounter++;
		if ( $giCounter > $gnPaginate ) {
			break;
		}
	}
	mysql_free_result($result);
	if ( $tmp ) {
		$gsHtml .= "<tr><td colspan=5 style='border-color: white; font-weight: bold; font-size: 1.4em;'>Multiply Occurring User Agents</td></tr>\n" .
		"<tr> <th>UA Profiler's Classification</th> <th>useragentstring.com's Classification</th> <th>user-agent-string.info's Classification</th> <th>Actual User-Agent</th> <th>#Tests</th> <th>Actions</th> </tr>\n" .
		"$tmp";
	}
}

$gsHtml .= "</table>\n";

echo $gAdminLinks;
?>

<form>
<p style="margin: 4px 0;">
pbrowser: <input type=text name=pbrowser value="<?php echo $gsPbrowser ?>">
&nbsp;&nbsp;&nbsp;
useragent: <input type=text name=useragent value="<?php echo $gsUseragent ?>">
&nbsp;&nbsp;&nbsp;
<input name=confirmed type=checkbox<?php echo ( $gbConfirmed ? " checked" : "" ) ?>> also show confirmed User Agents
&nbsp;&nbsp;&nbsp;
<input type=submit value="Submit">
</form>


<?php
$unconfirmed = doSimpleQuery("select count(distinct(useragent)) from $gTestsTable where confirmed=0;");
if ( $unconfirmed ) {
	echo "<p style='color: #B72E00;'>$unconfirmed unconfirmed User Agents</p>\n";
}
else {
	echo "<p style='color: #B72E00;'>All User Agents are confirmed.</p>\n";
}
?>

</div><div style='margin: 8px;'><form method=get><input type=submit value='Submit'><p>

<?php echo $gsHtml; ?>

<p><input type=submit value='Submit'>
<input type=hidden name=submit value=1>
<input type=hidden name=pbrowser value='<?php echo $gsPbrowser ?>'>
<input type=hidden name=useragent value='<?php echo $gsUseragent ?>'>
<input type=hidden name=confirmed value='<?php echo ( $gbConfirmed ? "on" : "" ) ?>'>
</form>

<script defer>
function onclickChange(testid, newPbrowser) {
	var box = document.getElementById("box" + testid);
	box.innerHTML = "<input type=text name='b_" + testid + "' value='" + newPbrowser + "'>";
}

<?php
$aIds = array_keys($ghDoubleCheck);
for ( $i = 0; $i < count($aIds); $i++ ) {
	$id = $aIds[$i];
	$ua = $ghDoubleCheck[$id];
	echo 
		"var domscript = document.createElement('script');\n" .
	    "domscript.src = 'parse-service.php?service=useragentstringcom&id=$id&ua=" . urlencode($ua) . "';\n" .
		"document.getElementsByTagName('head')[0].appendChild(domscript);\n" .
		"var domscript = document.createElement('script');\n" .
	    "domscript.src = 'parse-service.php?service=user-agent-string-info&id=b_$id&ua=" . urlencode($ua) . "';\n" .
		"document.getElementsByTagName('head')[0].appendChild(domscript);\n\n";
}
echo "</script>\n";

insertFooter(); 


function drawUseragent($num, $testid, $useragent, $pbrowser) {
	global $ghDoubleCheck;
	$newPbrowser = regexUseragentToBrowser($useragent);  // suggest the latest answer to parsing the useragent string
	$ghDoubleCheck[$testid] = $useragent;

	return "<tr> <td valign=top><nobr>$pbrowser</nobr></td> <td valign=top><nobr><span id=$testid>&nbsp;</span></nobr></td> <td valign=top><nobr><span id=b_$testid>&nbsp;</span></nobr></td> <td valign=top>$useragent</td> <td valign=top align=right>$num</td> <td valign=top>" .
		"<nobr><input name=rb_$testid type=radio value=con checked> Confirm</nobr><br>" .
		"<nobr><input name=rb_$testid type=radio value=rem> Remove</nobr><br> " .
		"<nobr><input name=rb_$testid type=radio value=cha onclick='onclickChange($testid, \"$newPbrowser\")'> Change</nobr><div id=box$testid></div>" .
		"</td></tr>\n";
}

function confirmUA($testid) {
	global $gTestsTable;

	$query = "select useragent, pbrowser, browser, v1, v2, v3 from $gTestsTable where testid=$testid;";
	$result = doQuery($query);
	$row = mysql_fetch_assoc($result);
	$command = "update $gTestsTable set pbrowser='" . $row['pbrowser'] . "'" .
		", browser='" . $row['browser'] . "'" .
		( NULL === $row['v1'] ? "" : ", v1=" . $row['v1'] ) .
		( NULL === $row['v2'] ? "" : ", v2=" . $row['v2'] ) .
		( NULL === $row['v3'] ? "" : ", v3='" . $row['v3'] . "'" ) . 
		", confirmed=1 where useragent='" . $row['useragent'] . "';";
	doSimpleCommand($command);
	mysql_free_result($result);
}


function removeUA($testid) {
	global $gTestsTable;

	$useragent = doSimpleQuery("select useragent from $gTestsTable where testid=$testid;");
	$command = "delete from $gTestsTable where useragent='$useragent';";
	echo "CVSNO: removeUA: $command<hr>\n";
	doSimpleCommand($command);
}


function changeUA($testid) {
	global $gTestsTable;

	$pbrowser = $_GET["b_$testid"];
	$browser = $v1 = $v2 = $v3 = NULL;
	if ( ereg("(.*) ([0-9]+)\.([0-9]+)\.([0-9]+)", $pbrowser, $regs) ) {
		$browser = $regs[1];
		$v1 = $regs[2];
		$v2 = $regs[3];
		$v3 = $regs[4];
	}
	else if ( ereg("(.*) ([0-9]+)\.([0-9]+)([ab][0-9]+[a-z]*)", $pbrowser, $regs) ) {
		$browser = $regs[1];
		$v1 = $regs[2];
		$v2 = $regs[3];
		$v3 = $regs[4];
	}
	else if ( ereg("(.*) ([0-9]+)\.([0-9]+)", $pbrowser, $regs) ) {
		$browser = $regs[1];
		$v1 = $regs[2];
		$v2 = $regs[3];
	}
	else if ( ereg("(.*) ([0-9]+)", $pbrowser, $regs) ) {
		$browser = $regs[1];
		$v1 = $regs[2];
		$v2 = $regs[3];
	}
	else {
		$browser = $pbrowser;
	}


	$useragent = doSimpleQuery("select useragent from $gTestsTable where testid=$testid;");
	$command = "update $gTestsTable set confirmed=1, pbrowser='$pbrowser', browser='$browser'" .
		( NULL === $v1 ? "" : ", v1=$v1" ) .
		( NULL === $v2 ? "" : ", v2=$v2" ) .
		( NULL === $v3 ? "" : ", v3='$v3'" ) .
		" where useragent='$useragent';";
	echo "CVSNO: changeUA: pbrowser='$pbrowser', browser='$browser', v1='$v1', v2='$v2', v3='$v3' from $useragent<br>\n";
	doSimpleCommand($command);
}

?>
